package stockblaster.client;

import java.util.HashMap;
import java.util.LinkedList;

import stockblaster.gen.R;

public class Companies {
	private static LinkedList<String> companyNames;
	private static HashMap<String, Double> sharesValue;
	private static HashMap<String, Integer> companyLogos;
	private static HashMap<String, Double> percentGrowths;
	private static HashMap<String, String> companyInfo;
	private static HashMap<String, Integer> companyArrow;
	
	private static HashMap<String, LinkedList<String[]>> companyNews;
	private static HashMap<String, String> headlineContent;
	private static LinkedList<String> newsHeadline;
	
	public static void initCompanies() {
		//companyInfo is initialized in SummaryActivity class.
		
		companyNames = new LinkedList<String>();
		sharesValue = new HashMap<String, Double>();
		companyLogos = new HashMap<String, Integer>();
		percentGrowths = new HashMap<String, Double>();
		companyInfo = new HashMap<String, String>();
		companyArrow = new HashMap<String, Integer>();
		
		companyNames.add("Free Inc.");
		sharesValue.put("Free Inc.", 422.00);
		companyLogos.put("Free Inc.", R.drawable.free_company);
		percentGrowths.put("Free Inc.", 0.3);
		companyArrow.put("Free Inc.", R.drawable.stock_up);
		
		companyNames.add("Earth Microsystems");
		sharesValue.put("Earth Microsystems", 236.00);
		companyLogos.put("Earth Microsystems", R.drawable.save_the_earth_company);
		percentGrowths.put("Earth Microsystems", 1.2);
		companyArrow.put("Earth Microsystems", R.drawable.stock_up);
		
		companyNames.add("Noxxe Mobil");
		sharesValue.put("Noxxe Mobil", 185.00);
		companyLogos.put("Noxxe Mobil", R.drawable.noxxe_mobile_company);
		percentGrowths.put("Noxxe Mobil", 0.7);
		companyArrow.put("Noxxe Mobil", R.drawable.stock_up);
		
		companyNames.add("Waterworks");
		sharesValue.put("Waterworks", 77.00);
		companyLogos.put("Waterworks", R.drawable.waterworks_company);
		percentGrowths.put("Waterworks", 0.1);
		companyArrow.put("Waterworks", R.drawable.stock_up);
		
		companyNames.add("Nanosoft");
		sharesValue.put("Nanosoft", 675.00);
		companyLogos.put("Nanosoft", R.drawable.nanosoft_company);
		percentGrowths.put("Nanosoft", 2.1);
		companyArrow.put("Nanosoft", R.drawable.stock_up);
		
		
		
		companyNews = new HashMap<String, LinkedList<String[]>>();
		newsHeadline = new LinkedList<String>();
		headlineContent = new HashMap<String, String>();
	}
	
	public static void updateCompany(String cName, Double sharePrice,
			Double growth, boolean isArrowUp) {
		Double newShareValue = 0.00;
		if (Companies.doesCompanyExist(cName)) {
			if (isArrowUp) {
				newShareValue = Companies.getCompanyShareValue(cName) *
						(1 + growth / 100);
			} else {
				newShareValue = Companies.getCompanyShareValue(cName) *
						(1 - growth / 100);
			}
			sharesValue.put(cName, newShareValue);
			//sharesValue.put(cName, sharePrice);
			percentGrowths.put(cName, growth);
			if (isArrowUp) {
				companyArrow.put(cName, R.drawable.stock_up);
			} else if (!isArrowUp) {
				companyArrow.put(cName, R.drawable.stock_down);
			}
		} else {
			companyNames.add(cName);
		}
		sharesValue.put(cName, newShareValue);
		//sharesValue.put(cName, sharePrice);
		percentGrowths.put(cName, growth);
		if (isArrowUp) {
			companyArrow.put(cName, R.drawable.stock_up);
		} else if (!isArrowUp) {
			companyArrow.put(cName, R.drawable.stock_down);
		}
	}
	
	public static void updateNews(String cName, String title, String news) {
		String[] ns = new String[2];
		ns[0] = title; ns[1] = news;
		LinkedList<String[]> temp = new LinkedList<String[]>();
		temp.add(ns);
		if (companyNews.containsKey(cName)) {
			/*LinkedList<String[]> previous = (LinkedList<String[]>) companyNews.get(cName);
			previous.add(ns);
			companyNews.put(cName, previous);*/
			companyNews.get(cName).add(ns);
		} else {
			companyNews.put(cName, temp);
		}
		newsHeadline.add(title);
		headlineContent.put(title, news);
	}
	
	public static void resetNewsData() {
		companyNews = new HashMap<String, LinkedList<String[]>>();
		newsHeadline = new LinkedList<String>();
		headlineContent = new HashMap<String, String>();
		//companyNews.clear();
		//newsHeadline.clear();
		//headlineContent.clear();
	}
	
	public static HashMap<String, LinkedList<String[]>> getCompanyNews() {
		return companyNews;
	}
	
	public static LinkedList<String> getCompanyNewsHeadlines() {
		return newsHeadline;
	}
	
	public static HashMap<String, String> getHeadlineContent() {
		return headlineContent;
	}
	
	/**
	 * Method to get up arrow or down arrow
	 * @param cName company name
	 * @return the id reference to the image
	 */
	public static Integer getArrow(String cName) {
		return companyArrow.get(cName);
	}
	
	/**
	 * Method to get company info
	 * @param cName company name
	 * @return the company info in String
	 */
	public static String getInfo(String cName) {
		return companyInfo.get(cName);
	}
	
	/**
	 * Method to set the company info
	 * @param cName the company name
	 * @param info the info about the company
	 */
	public static void setCompanyInfo(String cName, String info) {
		companyInfo.put(cName, info);
	}
	
	/**
	 * Method to set the company growth image
	 * @param cName the company name
	 * @param isStockUp true if the company is growing; false otherwise
	 */
	public static void setArrow(String cName, boolean isStockUp) {
		if (isStockUp) {
			companyArrow.put(cName, R.drawable.stock_up);
		} else if (!isStockUp) {
			companyArrow.put(cName, R.drawable.stock_down);
		}
	}
	
	/**
	 * Method to check whether a company exists
	 * @param cName the company name
	 * @return true if company exists; false otherwise
	 */
	public static boolean doesCompanyExist(String cName) {
		if (companyNames.contains(cName)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Method to return a company share value
	 * @param cName the company name
	 * @return the value of the share; null if company not found
	 */
	public static Double getCompanyShareValue(String cName) {
		return sharesValue.get(cName);
	}
	
	/**
	 * Method to return a company percent growth
	 * @param cName the company name
	 * @return the percent growth of the company; null if company not found
	 */
	public static Double getCompanyPercentGrowth(String cName) {
		return percentGrowths.get(cName) != null ? 
				percentGrowths.get(cName) : 0.00;
	}
	
	/**
	 * Method to get all the company names
	 * @return the company names
	 */
	public static LinkedList<String> getCompanyNames() {
		return companyNames;
	}
	
	/**
	 * Method to get the company logo
	 * @param cName the company name
	 * @return the id reference to the company logo; null if not found
	 */
	public static int getCompanyLogo(String cName) {
		return companyLogos.get(cName);
	}
}
